String

A string is usually a bit of text in programming that is written to be displayed to users. It is known to Python when you want to display a string. This is because programmers use either double quote " or single quote ' to enclose a word or group of words to express a string.

'Hello' is the same as "Hello".

Assign a string To a variable

Various String Operators
There are various string operators that can be used in different ways.
Suppose if a=Hello. Similarly, if you are using a*2, it will "HelloHello". Likewise, you can use other operators in string.

 Operators Descriptions Example 
 []Slice- it gives the letter from the given indexx[1] will give "e" from the word Guru as such (0=H, 1=e, 2=l and 3=l)
x="Hello"
print (x[1])
 [:]Range slice-it gives the characters from the given rangex[1:3] it will give "el" from the word Hello.
x="Hello"
print (x[1:3])
 inreturns true if a letter exist in the given string e is present in word Guru and hence it will give 1 (True) x="Hello"
print ("e" in x)
 not in
returns true if a letter exist is not in the given stringu is not present in word Hello and hence it will give 1x="Hello"
print ("u" not in x)
 r/R   
 % - Used for string format   
 * RepeatIt prints the characters twice
 x="Hello"
print ("u" not in x)

Accessing characters in python
Strings in Python are arrays of bytes representing uni-code characters.However, Python does not have a character data type, a single character is simply a string with a length of 1. Individual characters of a String can be accessed by using the method of Indexing. Square brackets can be used to access elements of the string. Get the character at position 1 (remember that the first character has the position 0). Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character and so on. While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types will cause a TypeError.
a = "Hello, World!"
print(a[1])
OutPut:-
e  
a = "Hello, World"
print(a[-1])
OutPut:-
d

String Slicing
Substring. Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])
llo
Print the element between 6 to 2nd last elemnt
print(b[6:-1])
 
Deleting/Updating from a String
In Python, Updation or deletion of characters from a String is not allowed. This will cause an error because item assignment or item deletion from a String is not supported. Although deletion of entire String is possible with the use of a built-in del keyword. This is because Strings are immutable, hence elements of a String cannot be changed once it has been assigned. Only new strings can be reassigned to the same name.
a = "Hello, World!"
print(a)
a[2]='h'


a = "Hello, World!"
print(a)
a ="How are You"
print(a)

The len() methods retrun the length of a String 
a = "Hello, World!"
print(len(a))

length = (len(a))
last=a[length-1]
print(last)
String Reverse
lang = “Python”
print(‘’.join(reversed(lang)))
The lower() method returns the string in lower case.
a = "Hello, World!"
print(a.lower())

The upper() method returns the string in upper case.
a = "Hello, World!"
print(a.upper())

 
The replace() method replaces a string with another string.
a = "Hello, World!"
print(a.replace("H", "J"))
OutPut:-
Hello  World

The strip() method removes any whitespace from the beginning or the end.
a = " Hello, World! "
print(a.strip())

The split() methods splits the string into substring
a = "Hello, World!"
print(a.split(","))
['Hello', ' World!']

String compare 
s1 ="Python"
s2 ="Python"
if(s1 == s2):
print("Both strings are equal")
else:
print("Both strings are not equal")
Both strings are equal
s1 ="Python"
s2 ="Python-3"
if(s1 == s2):
print("Both strings are equal")
else:
print("Both strings are not equal")
strings are not equal

Check String
To check if a certain phrase or character is present in a string, we can use the Both keywords in or not in.

Check if the phrase "rain" is present in the following text:
txt = "The rain in Spain stays mainly in the plain"
x = "rain" in txt
print(x)
True
 
Check if the phrase "rain" is not present in the following text:
txt = "The rain in Spain stays mainly in the plain"
x = "rain" not in txt
print(x)
False

String Concatenation
Merge variable a with variable b into variable c:
a = "Hello "
b = " World"
c = a + b
print(c)
String Replace 
string.replace(old, new, count)
# Python3 program to demonstrate the
string = "Hello Hello Hello Hello Hello Hello"

# Prints the string by replacing geeks by Geeks
print(string.replace("Hello", "Welcome"))

# Prints the string by replacing only 3 occurrence of Geeks
print(string.replace("Hello", "Welcome", 3))

No comments:

Post a Comment